<template>
  <div class="app-container">
    <el-form :model="form" ref="form" :inline="true">
      <el-form-item label="商品名称" prop="productName">
        <el-input v-model="form.productName" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="orderStatus">
        <el-select v-model="form.orderStatus" filterable clearable>
          <el-option
            v-for="item in dict.type.ims_produce_order_status"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="关联单号" prop="reOrderNo">
        <el-input v-model="form.reOrderNo" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="主料规格" prop="masterSkuId">
        <el-select v-model="form.masterSkuId" filterable clearable>
          <el-option
            v-for="item in bomList"
            :key="item.skuId"
            :label="item.skuName"
            :value="item.skuId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="生产日期" prop="time">
        <el-date-picker v-model="form.time" type="daterange" start-placeholder="开始日期" end-placeholder="结束日期"
                        value-format="yyyy-MM-dd" clearable></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" plain icon="el-icon-download"
               v-hasPermi="['ims:produceOrder:export']" @click="handleExport">导出
    </el-button>
    <el-button type="success" plain icon="el-icon-download"
               v-hasPermi="['ims:produceOrder:zip']" @click="handleZip">打包下载
    </el-button>
    <el-button type="primary" plain icon="el-icon-scissors"
               v-hasPermi="['ims:produceOrder:start']" @click="handleCut">批量裁剪
    </el-button>

    <el-table
      v-loading="loading"
      :data="list"
      @selection-change="handleChange"
      row-key="id"
    >
      <el-table-column type="selection" width="50"></el-table-column>
      <el-table-column type="index" label="序号" align="center">
        <template slot-scope="scope">
          <span>{{ (form.pageNum - 1) * form.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="orderNo" label="生产单号" align="center">
        <template slot-scope="scope">
          <el-link type="primary" @click="updateRow(1,scope.row)">{{ scope.row.orderNo }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="produceType" label="生产来源" align="center" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ims_produce_type" :value="scope.row.produceType"/>
        </template>
      </el-table-column>
      <el-table-column prop="reOrderNo" label="关联单号" align="center"/>
      <el-table-column prop="productName" label="商品名称" align="center"/>
      <el-table-column prop="skuDescCn" label="商品规格" align="center" width="200"/>
      <el-table-column prop="customerName" label="客户名称" align="center" width="100"/>
      <el-table-column prop="logo" label="Logo" align="center" width="100"/>
      <el-table-column prop="masterSkuDesc" label="主材料规格" align="center" width="100"/>
      <el-table-column prop="quantity" label="订单数量" align="center" width="80"/>
      <el-table-column prop="orderStatus" label="订单状态" align="center" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ims_produce_order_status" :value="scope.row.orderStatus"/>
        </template>
      </el-table-column>
      <el-table-column prop="priority" label="优先级" align="center" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ims_priority_level" :value="scope.row.priority"/>
        </template>
      </el-table-column>
      <el-table-column prop="processStepList" label="完成工序" align="center" width="100">
        <template slot-scope="scope">
          <div v-for="val in scope.row.processStepList">
            <div v-if="scope.row.currentStep>=val.stepNo" style="color: #0275a1">{{ val.proName }}</div>
            <div v-else>{{ val.proName }}</div>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="updateTime" label="更新时间" align="center" width="150"/>
      <el-table-column label="操作" align="center" width="120">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="updateRow(1,scope.row)"
          >详情
          </el-button>
          <!--          待生产-->
<!--          <el-button-->
<!--            v-if="scope.row.orderStatus==10"-->
<!--            type="text"-->
<!--            v-hasPermi="['ims:produceOrder:start']"-->
<!--            @click="updateRow(2,scope.row)"-->
<!--          >开始-->
<!--          </el-button>-->
          <el-button
            v-if="scope.row.orderStatus==10"
            type="text"
            v-hasPermi="['ims:produceOrder:cancel']"
            @click="updateRow(5,scope.row)"
          >取消
          </el-button>
          <el-button
            v-if="scope.row.orderStatus==10"
            type="text"
            v-hasPermi="['ims:produceOrder:download']"
            @click="updateRow(4,scope.row)"
          >图纸下载
          </el-button>
          <el-button
            v-if="scope.row.orderStatus==10"
            type="text"
            v-hasPermi="['ims:produceOrder:print']"
            @click="updateRow(6,scope.row)"
          >标签打印
          </el-button>
          <el-button
            v-if="scope.row.orderStatus==20"
            type="text"
            v-hasPermi="['ims:produceOrder:work']"
            @click="updateRow(3,scope.row)"
          >报工
          </el-button>
          <el-button
            v-if="scope.row.orderStatus==30"
            v-hasPermi="['ims:produceOrder:store']"
            type="text"
            @click="updateRow(7,scope.row)"
          >入库
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="total"
      :page.sync="form.pageNum"
      :limit.sync="form.pageSize"
      @pagination="getList"
    />
    <detailDialog ref="detailDialog" @ok="handleQuery"></detailDialog>
    <printDialog ref="printDialog"></printDialog>
    <inDialog ref="inDialog" @ok="handleQuery"></inDialog>
  </div>
</template>

<script>
import {
  cancelProduceOrder, listProduceBom,
  listProduceOrder, startProduceOrder, storeProduceOrder
} from "../../../api/imsMng";
import detailDialog from "./detailDialog.vue";
import printDialog from "./printDialog.vue";
import inDialog from "./reportDialog.vue";
import {formatDay} from "../../../utils";

export default {
  components: {detailDialog, printDialog, inDialog},
  dicts: ['ims_produce_order_status', 'sys_yes_no', 'ims_priority_level', 'ims_produce_type'],
  data() {
    return {
      loading: false,
      selectList: [],
      list: [],
      form: {
        productName: "",
        orderStatus: "",
        reOrderNo: "",
        masterSkuId: "",
        time: [],
        pageNum: 1,
        pageSize: 10
      },
      bomList: [],
      total: 0
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      this.handleQuery()
      listProduceBom({master: 0, pageSize: -1}).then(res => {
        this.bomList = res.data
      })
    },
    handleQuery() {
      this.form.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('form')
      this.handleQuery()
    },
    handleExport() {
      this.download('/ims/produce/order/export', {
        ...this.form
      }, `生产订单_${formatDay(new Date())}.xlsx`)
    },
    handleZip() {
      if (!this.selectList.length) {
        this.$message.error('请先选择至少一条数据')
        return
      }
      let ids = []
      this.selectList.map(val => {
        ids.push(val.id)
      })
      this.download('/ims/produce/order/zip', {
        ids: ids
      }, `生产订单_${formatDay(new Date())}.zip`)
    },
    handleCut() {
      if (!this.selectList.length) {
        this.$message.error('请先选择至少一条数据')
        return
      }
      let ids = []
      this.selectList.map(val => {
        ids.push(val.id)
      })
      startProduceOrder({ids: ids}).then(res => {
        this.handleQuery()
        this.$message.success('批量裁剪成功')
      })
    },
    handleChange(e) {
      this.selectList = e
    },
    getList() {
      this.loading = true
      if (this.form.time && this.form.time.length) {
        this.form.startTime = this.form.time[0]
        this.form.endTime = this.form.time[1]
      } else {
        this.form.startTime = ''
        this.form.endTime = ''
      }
      listProduceOrder(this.form).then(res => {
        this.list = res.rows
        this.total = res.total
      }).finally(e => {
        this.loading = false
      })
    },
    updateRow(flag, row) {
      if (flag == 1) {
        this.$refs.detailDialog.show(row, 2)
      }
      if (flag == 2) {

      }
      if (flag == 3) {
        this.$refs.inDialog.show(row)
      }
      if (flag == 4) {
        if (row.imageUrl) {
          window.open(row.imageUrl)
        } else {
          this.$message.error('暂无图纸信息')
        }
      }
      if (flag == 5) {
        this.$modal.confirm('是否确定取消').then(() => {
          cancelProduceOrder({id: row.id}).then(res => {
            this.handleQuery()
            this.$message.success('取消成功')
          })
        })
      }
      if (flag == 6) {
        this.$refs.printDialog.show(row.id)
      }
      if (flag == 7) {
        this.$modal.confirm('是否确定入库').then(() => {
          storeProduceOrder({id: row.id}).then(res => {
            this.handleQuery()
            this.$message.success('入库成功')
          })
        })
      }
    }
  }
}
</script>
